#include "startup.h"
#include "mainui.h"
#include "systemui.h"
#include "hisdataui.h"
#include "paramui.h"
#include "errorui.h"
#include "paramsetui.h"
#include "lightingui.h"
#include "log4z.h"
#include "rs232.h"

using namespace zsummer::log4z;
int MAJORVER = 1;
int MINORVER = 0;
int REVIVER = 1;
int IMODE = 1;
#include <QApplication>
#include <QTranslator>
#include <QFile>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //install translator
    QTranslator translator;
    bool b = false;
    b = translator.load("cn.qm");
    a.installTranslator(&translator);

    //start log4z
    ILog4zManager::getRef().start();
    ILog4zManager::getRef().setLoggerPath(LOG4Z_MAIN_LOGGER_ID, "/home/root/log/");
    ILog4zManager::getRef().setLoggerLevel(LOG4Z_MAIN_LOGGER_ID, LOG_LEVEL_TRACE);
    ILog4zManager::getRef().setLoggerOutFile(LOG4Z_MAIN_LOGGER_ID, true);

    //load version
    QFile file("/home/root/ver.txt");
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        //error
        LOGFMTD("Can't find version file");
    }
    //... (open the file for reading, etc.)
    QTextStream textStream(&file);
    QString qVersion = textStream.readAll();

    LOGFMTD("Current version text %s", qVersion.toStdString().c_str());
    QStringList verDetail = qVersion.split(".");
    if (verDetail.size() == 3)
    {
        //ok
        MAJORVER = verDetail.at(0).toInt();
        MINORVER = verDetail.at(1).toInt();
        REVIVER = verDetail.at(2).toInt();
    }

    int cport_nr = 0,       /* /dev/ttyS1 (COM1 on windows) */
    bdrate= 57600;          /* 57600 baud */
    char mode[] = {'8','N','1',0};
    int iSerial = RS232_OpenComport(cport_nr, bdrate, mode);
    int iRetryTms = 0;
    while(iSerial != 0 && iRetryTms++ <= 3)
    {
        //retry
        iSerial = RS232_OpenComport(cport_nr, bdrate, mode);
    }
    //fatal error
    if (iSerial != 0)
    {
        return 0;
    }
    //All UIs
    StartUp suUi;
    MainUI mu;
    HisDataUI hisUi;
    SystemUI sysUi;
    ParamUI pmUi;
    ParamSetUI pmstUi;
    ErrorUI erUi;
    LightingUI lgtUi;

    //startup to main
    QObject::connect(&suUi,SIGNAL(sendShow()),&mu,SLOT(getShow()));
    QObject::connect(&suUi,SIGNAL(powerOff()),&lgtUi,SLOT(onShutdown()));

    //main to three UI
    QObject::connect(&mu,SIGNAL(sendHis()),&hisUi,SLOT(getShow()));
    QObject::connect(&mu,SIGNAL(sendSys()),&sysUi,SLOT(getShow()));
    QObject::connect(&mu,SIGNAL(sendPar()),&pmUi,SLOT(getShow()));
    //UI back to main
    QObject::connect(&pmUi,SIGNAL(retMain()),&mu,SLOT(getShow()));
    QObject::connect(&sysUi,SIGNAL(retMain()),&mu,SLOT(getShow()));
    QObject::connect(&hisUi,SIGNAL(retMain()),&mu,SLOT(getShow()));
    QObject::connect(&lgtUi,SIGNAL(retMain()),&mu,SLOT(getShow()));

    //param to light
    QObject::connect(&pmUi,SIGNAL(paramSet()),&pmstUi,SLOT(getShow()));
    QObject::connect(&pmstUi,SIGNAL(resetParam()),&pmUi,SLOT(getShow()));
    QObject::connect(&pmstUi,SIGNAL(sendLight()),&lgtUi,SLOT(getShow()));

    //error
    QObject::connect(&suUi,SIGNAL(sendErr(QString)),&erUi,SLOT(getShow(QString)));
    QObject::connect(&lgtUi,SIGNAL(sendErr(QString)),&erUi,SLOT(getShow(QString)));
    QObject::connect(&erUi,SIGNAL(sendQuit()),&a,SLOT(quit()));

    suUi.show();

    int iRet =  a.exec();

    //close
    RS232_CloseComport(cport_nr);
    return iRet;
}
